package com.linewell.dao;

import com.linewell.entity.Article;
import com.linewell.entity.Focus;
import com.linewell.entity.Tag;
import com.linewell.entity.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Title: ssm2
 * @description:
 * @author: hyuhuang@linewell.com
 * @since:2018/1/18 16:43
 */
public interface FocusMapper {

    @ResultMap("com.linewell.dao.FocusMapper.Focus")
    @Select("select * from focus f left join userinfo u on u.user_id = f.focus_id where f.user_id=${userId};")
    Focus selectAllFocus(@Param("userId") Long userId);

    @ResultMap("com.linewell.dao.FocusMapper.FocusMap")
    @Select("select top 5 f.focus_id,nick_name from focus f,userinfo u where f.user_id=u.user_id and f.focus_id = ${userId};")
    Focus selectFive(@Param("userId")long userId);

    @Insert("insert into focus values ('${focusId}','${userId}')")
    boolean attention(@Param("userId")Long userId, @Param("focusId")Long focusId);

    @Select("select * from focus where user_id = ${userId} AND focus_id = ${focusId}")
    List<Focus> selectAttention(@Param("userId")Long userId, @Param("focusId")Long focusId);



    @Select("select * from userinfo where user_id = ${userId}")
    UserInfo selectFocus(@Param("userId") Long userId);

    @Select("select * from userinfo where user_id = ${focusId}")
    UserInfo selectFans(@Param("focusId")Long focusId);

    @Update("update userinfo set focus_num = ${focusNum} where user_id = ${userId}; " +
            "update userinfo set fans_num  = ${fansNum} where user_id = ${focusId};")
    void updateFocusAndFans(@Param("focusNum")int focusNum, @Param("fansNum")int fansNum, @Param("userId")Long userId, @Param("focusId")Long focusId);

    @Select("select distinct t.* from tag t,multilabel m where m.lable_id=${userId} and m.tag_id = t.tag_id;")
    List<Tag> selectUserTag(@Param("userId")Long userId);

    @ResultMap("com.linewell.dao.ArticleMapper.Article")
    @Select("select * from article a,multilabel m,userinfo u,tag t " +
            "where m.type=2 " +
            "and m.tag_id = ${tagId} " +
            "and t.tag_id=m.tag_id  " +
            "and m.lable_id = a.article_id " +
            "and a.user_id = u.user_id " +
            "order by newid() Offset 0 Row Fetch Next 1 Rows Only")
    Article recommendArticle(@Param("tagId")int tagId);

    @ResultMap("com.linewell.dao.ArticleMapper.Article")
    @Select("select * from article a,userinfo u,tag t,multilabel m " +
            "where lable_id = article_id  " +
            "and a.user_id=u.user_id  " +
            "and t.tag_id=m.tag_id  " +
            "and m.type=2 " +
            "and a.acheck =1" +
            "order by newid() Offset 0 Row Fetch Next ${tagId} Rows Only")
    List<Article> selectAllArticle(@Param("tagId")int tagId);
}
